home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / B-C / C++ FAQ Reference 1.0 / C++ FAQ Reference 1.0.rsrc / TEXT_829.txt < prev    next >
Encoding:
Text File  |  1993-06-30  |  826 b   |  25 lines

  1. This will NOT work, since comments are parsed before the macro is expanded:
  2.     #ifdef DEBUG_ON
  3.       #define  DBG
  4.     #else
  5.       #define  DBG  //
  6.     #endif
  7.     DBG cout << foo;
  8.  
  9. This is the simplest technique:
  10.     #ifdef DEBUG_ON
  11.       #define  DBG(anything)  anything
  12.     #else
  13.       #define  DBG(anything)  /*nothing*/
  14.     #endif
  15.  
  16. Then you can say:
  17.     //...
  18.     DBG(cout << "the value of foo is " << foo << '\n');
  19.     //                                                ^-- ';' outside ()
  20.  
  21. Any commas in your 'DBG()' statement must be enclosed in a '()':
  22.     DBG(i=3, j=4);    //<---- C-preprocessor will generate error message
  23.     DBG(i=3; j=4);    //<---- ok
  24.  
  25. There are also more complicated techniques that use variable argument lists, but these are primarily useful for 'printf()' style (see question on the pros and cons of <iostream.h> as opposed to <stdio.h> for more).